package edu.ucsur.tp.contactcenter.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.ucsur.tp.beans.ClienteBean;
import edu.ucsur.tp.beans.UsuarioBean;
import edu.ucsur.tp.negocio.mantenimientos.ClienteNego;

/**
 * Servlet implementation class ClientesServlet
 */
@WebServlet("/ClientesServlet")
public class ClientesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClientesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String action = request.getParameter("action");
		String code = request.getParameter("code");
		
		response.setContentType( "text/html" );
		RequestDispatcher dispatcher; 
		ClienteBean cli = new ClienteBean();
		ClienteNego oCli = new ClienteNego();
		
		switch (action){
		
			case "new":
				request.setAttribute("action", action);
				request.setAttribute("cliente", cli);
				dispatcher = request.getRequestDispatcher("clientes_registro.jsp");
				dispatcher.forward(request, response);
				break;
			
			case "edit":
				cli = oCli.obtener(code);
				request.setAttribute("action", action);
				request.setAttribute("cliente", cli);
				dispatcher = request.getRequestDispatcher("clientes_registro.jsp");
				dispatcher.forward(request, response);
				break;
				
			case "delete":
				oCli.eliminar(code);
				response.sendRedirect("clientes_lista.jsp");
				break;
		
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String action = request.getParameter("action");
		response.setContentType( "text/html" );
		ClienteBean cli = new ClienteBean();
		ClienteNego oCli = new ClienteNego();
		
		cli.setId_cliente(Integer.parseInt(request.getParameter("txtId")));
		cli.setApellidos(request.getParameter("txtApellidos"));
		cli.setNombres(request.getParameter("txtNombres"));
		cli.setNrodoc(request.getParameter("txtDocumento"));
		cli.setCorreo(request.getParameter("txtCorreo"));
		cli.setTelefono1(request.getParameter("txtTelefono1"));
		cli.setFechanac(request.getParameter("txtFechaNac"));
			
		cli.setGenero("M");
		cli.setId_pais("PE");
		cli.setId_ubigeo(5);
		
		try {
			HttpSession sesion = request.getSession(true);
			UsuarioBean user = (UsuarioBean)sesion.getAttribute("UserLogin");
			cli.setUsuarioreg(user.getId_usuario());
		} catch (Exception e) {
			cli.setUsuarioreg("RPADILLA");
		}
		
		switch (action){
			case "new":
				oCli.insertar(cli);
				break;
				
			case "edit":
				oCli.modificar(cli);
				break;
		}
		response.sendRedirect("clientes_lista.jsp");
	}

}
